<?php namespace Admin\Controller;
use Common\Model\Commod;
use Common\Tag\Common;

class CommodController extends CommonController{
    private $model;
    public function __auto(){
        $this->model=new Commod();
    }
    public function index(){
        $oldData=$this->model->get();
        View::with('oldData',$oldData);
        View::make();
    }

    public function addCommod(){
        if(IS_POST){
//            p(Q('post.'));die;
            if($this->model->store()){
                View::success('商品添加成功',U('index'));
            }else{
                View::error($this->model->getError());
            }
        }
        $cate=new \Common\Model\Cate();
        $cateData=Data::tree($cate->get(),'cname');
        $brand=new \Common\Model\Brand();
        $brandData=$brand->get();
//        p($cateData);die;
        View::with('brandData',$brandData);
        View::with('cateData',$cateData);
        View::make();
    }

//    删除
    public function delCommod(){
        $coid=Q('get.coid',0,'intval');
        //        删除列表图片
        $oldlistImg=$this->model->where("coid={$coid}")->lists('colistimg');
        if($oldlistImg[0]){
            unlink($oldlistImg[0]);
        }
        $commodetailModel=new \Common\Model\Commoddetil();
//        删除商品相册图片
        $oldcoImg=$commodetailModel->where("shop_commodity_coid={$coid}")->lists('coimg');
        if($oldcoImg[0]){
            unlink($oldcoImg[0]);
        }
//        p($oldcoImg);die;
//        删除商品详细表
        $commodetailModel->where("shop_commodity_coid={$coid}")->delete();
//         删除商品属性表
        $coattributeModel=new \Common\Model\Coattribute();
        $coattributeModel->where("shop_commodity_coid={$coid}")->delete();
//        删除商品表
        $this->model->where("coid={$coid}")->delete();
        View::success('删除成功');
        View::make();
    }


//    编辑
    public function editCommod(){
        if(IS_POST){
//            p(Q('post.'));die;
            if(!$this->model->edit()) View::error($this->model->getError());
//            die;
            View::success("修改成功",'index');
        }
//        获取基本信息
        $coid=Q('get.coid',0,'intval');
//        p($coid);die;
        $oldcommodData=Db::table('commodity co')->join('commdetail cd','co.coid','=','cd.shop_commodity_coid')
           ->where("coid={$coid}")->first();
//        如果图片是多张，就拆分字符串变成数组
        $oldcommodData['coimg']=explode('|',$oldcommodData['coimg']);
//        获取分类
        $cateModel=new \Common\Model\Cate();
        $cateData=Data::tree($cateModel->get(),'cname');

//        获取品牌
        $brandModel=new \Common\Model\Brand();
        $brandData=$brandModel->get();


//        获取商品属性
        $typeattributeModel=new \Common\Model\Tatt();
        $typeattributeData=$typeattributeModel->join('coattribute coa','taid','=','coa.shop_typeattribute_taid')
            ->where("tatype=1")->where("shop_commodity_coid={$coid}")->get();
        //获得taid就是为了以后排除已经选过的类型属性
        $taid = [];
//        更改获取属性列表
          foreach($typeattributeData as $k=>$u){
              $typeattributeData[$k]['tavalue']=explode(',',$u['tavalue']);
              $taid[]=$u['shop_typeattribute_taid'];
          }
//            p($taid);die;
        //把商品没有选过属性也拿取出来
        $typestr=implode(',',$taid);
//        p($typestr);die;
        $typeattr=$typeattributeModel->where("taid NOT IN ({$typestr}) AND shop_type_tid={$oldcommodData['shop_type_tid']}")
           ->where("tatype=1")
            ->get();
//        p($typeattr);die;

        foreach($typeattr as $t ){
            $typeattributeData[]=array(
                'coattvalue'=>'',
                'taname'=>$t['taname'],
                'tavalue'=>explode(',',$t['tavalue']),
                'tatype'=>$t['tatype'],
                'taid'=>$t['taid']
            );
        }



//        获取商品规格
        $typeattributeDatap=$typeattributeModel->join('coattribute coa','taid','=','coa.shop_typeattribute_taid')
            ->where("shop_commodity_coid={$coid}")->where("tatype=0")->get();
//        更改获取规格列表
        foreach($typeattributeDatap as $k=>$u){
            $typeattributeDatap[$k]['tavalue']=explode(',',$u['tavalue']);
        }
//        p($oldcommodData);die;
        View::with('cateData',$cateData);
        View::with('brandData',$brandData);
        View::with('typeattributeData',$typeattributeData);
        View::with('typeattributeDatap',$typeattributeDatap);
        View::with('oldcommodData',$oldcommodData);
        View::make();
    }

    //uploadify异步上传
    public function upload()
    {
        $file = Upload::path('Upload/Content/' . date('y/m'))->make();
        if (empty($file)) {
            // 相当于：echo json_encode(Upload::getError());exit;
            $this->ajax(Upload::getError());
        } else {
            /** $file内部就是以下这个数组
             * $file = array(
             * 0 => array(
             * 'path' => 'Upload/Content/15/8/123981239172.jpg',
             * 'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
             * 'image' => 1
             * ),
             * );**/
            //上传成功，把上传好的信息返给js 也就是uploadify
            $data = $file[0];
            // 相当于：echo json_encode($data);exit;
            $this->ajax($data);
        }
    }

//    异步删除图片
    public function deleteimg(){
        $path=Q('post.path');
        unlink($path);
    }


//    异步添加
    public function add(){
        $tid=Q('post.t',0,'intval');
//        p($tid);die;
        $typeAttribute=new \Common\Model\Tatt();
        $data=$typeAttribute->where("shop_type_tid={$tid}")->get();
        foreach($data as $k=>$u){
            //把字符串变为数组，将来好循环
            $data[$k]['tavalue'] = explode(',', $u['tavalue']);
        }
        echo json_encode($data);exit;
    }


}